package com.uidp.ui;

import java.util.ArrayList;
import java.util.List;

import com.acsior.sbc.exception.BusinessException;
import com.acsior.sbc.exception.SearchException;
import com.acsior.wu.bean.MBGeneralEditableObject;
import com.acsior.wu.bean.utils.MBManager;
import com.uidp.domain.Attributes;
import com.uidp.domain.Modul;
import com.uidp.domain.ModulModulElements;
import com.uidp.uitils.ManagedBeans;
import com.uidp.uitils.NavOutcomes;

public class MBModul extends MBGeneralEditableObject<Modul> {

	private List<ModulModulElements> modulMElements;

	private int selectedTableGroup;

	private int selectedTableAttribute;

	private boolean saveModul;

	public MBModul() {
		super(Modul.class);
		if (getGeneral().isEditMode()) {
			setSaveModul(true);
			try {
				this.modulMElements = ModulModulElements
						.searchModulElements(getGeneral());
				for (ModulModulElements m : getModulMElements()) {
					m.getModulElements().setAttributeses(
							Attributes.searchAttributes(m.getModulElements()));

				}

			} catch (BusinessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SearchException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

	@Override
	public void initData() {
		this.modulMElements = new ArrayList<ModulModulElements>();
	}

	/**
	 * Запис на модул
	 */
	public void saveModul() {
		if (isValid()) {
			try {
				getGeneral().store();
				setSaveModul(true);
			} catch (BusinessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	protected boolean isValid() {
		// TODO Auto-generated method stub
		return true;
	}

	/**
	 * Запис на модул
	 */
	public String newGroup() {
		getMBGroup().setModul(getGeneral());
		return navigate(NavOutcomes.group);
	}

	public String newAttribute() {
		return navigate(NavOutcomes.attribute);
	}

	public String navToEditGroup() {
		return "";
	}

	public String navToEditAttribute() {
		return "";
	}

	public String removeGroup() {
		return "";
	}

	public String removeAttribute() {
		return "";
	}

	/**
	 * @see MBGeneral#getManagedProp()
	 */
	@Override
	protected ManagedBeans getManagedProp() {
		return ManagedBeans.regModul;
	}

	public String getModulname() {
		return getGeneral().getModulname();
	}

	public void setModulname(String modulname) {
		getGeneral().setModulname(modulname);
	}

	public String getDescription() {
		return getGeneral().getDescription();
	}

	public void setDescription(String description) {
		getGeneral().setDescription(description);
	}

	public boolean isSaveModul() {
		return saveModul;
	}

	public void setSaveModul(boolean saveModul) {
		this.saveModul = saveModul;
	}

	/**
	 * Извлича от FacesContext-а MBGroup
	 * 
	 * @return връща MBGroup
	 */
	protected MBGroup getMBGroup() {
		return (MBGroup) MBManager.findBeanInCtx(ManagedBeans.group);
	}

	public List<ModulModulElements> getModulMElements() {
		return modulMElements;
	}

	public void setModulMElements(List<ModulModulElements> modulMElements) {
		this.modulMElements = modulMElements;
	}

	public int getSelectedTableGroup() {
		return selectedTableGroup;
	}

	public void setSelectedTableGroup(int selectedTableGroup) {
		this.selectedTableGroup = selectedTableGroup;
	}

	public int getSelectedTableAttribute() {
		return selectedTableAttribute;
	}

	public void setSelectedTableAttribute(int selectedTableAttribute) {
		this.selectedTableAttribute = selectedTableAttribute;
	}

}
